package com.warm.middle.websocket;

import com.alibaba.fastjson.JSON;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

/**
 * @Description websocket 解码器, 发送消息的时候可以直接发送 Object 对象
 * @Author iyb-liangchuang
 * @Date 2021/6/17 10:51
 */
public class WSEncoder implements Encoder.Text<WSMsg> {

    @Override
    public String encode(WSMsg wsMsg) throws EncodeException {
        try {
            return JSON.toJSONString(wsMsg);
        } catch (Exception e){
            return "msg convert error...";
        }
    }

    @Override
    public void init(EndpointConfig endpointConfig) {
    }

    @Override
    public void destroy() {
    }
}
